<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>16进制转换工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', 'Segoe UI', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            padding: 40px;
            max-width: 800px;
            width: 100%;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 10px;
            font-size: 28px;
        }

        .subtitle {
            text-align: center;
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }

        .input-group {
            margin-bottom: 25px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 600;
            font-size: 14px;
        }

        input[type="text"] {
            width: 100%;
            padding: 15px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 16px;
            font-family: 'Consolas', 'Monaco', monospace;
            transition: all 0.3s;
        }

        input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .button-group {
            display: flex;
            gap: 15px;
            margin-bottom: 25px;
        }

        button {
            flex: 1;
            padding: 15px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn-convert {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-convert:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn-clear {
            background: #f5f5f5;
            color: #666;
        }

        .btn-clear:hover {
            background: #e0e0e0;
        }

        .result-box {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 15px;
        }

        .result-label {
            color: #666;
            font-size: 12px;
            margin-bottom: 5px;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .result-value {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 18px;
            color: #333;
            word-break: break-all;
            padding: 10px;
            background: white;
            border-radius: 5px;
            border: 1px solid #e0e0e0;
        }

        .result-value.highlight {
            background: #e8f5e9;
            border-color: #4caf50;
            color: #2e7d32;
            font-weight: 600;
            font-size: 24px;
        }

        .example {
            background: #fff3cd;
            border-left: 4px solid #ffc107;
            padding: 15px;
            margin-top: 20px;
            border-radius: 5px;
        }

        .example-title {
            font-weight: 600;
            color: #856404;
            margin-bottom: 8px;
        }

        .example-content {
            color: #856404;
            font-size: 14px;
            line-height: 1.6;
        }

        .error {
            background: #ffebee;
            border-left: 4px solid #f44336;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
            color: #c62828;
            display: none;
        }

        .steps {
            background: #e3f2fd;
            border-radius: 10px;
            padding: 20px;
            margin-top: 20px;
        }

        .step {
            margin-bottom: 15px;
            padding-left: 30px;
            position: relative;
        }

        .step::before {
            content: '→';
            position: absolute;
            left: 0;
            color: #1976d2;
            font-weight: bold;
            font-size: 20px;
        }

        .step-title {
            font-weight: 600;
            color: #1565c0;
            margin-bottom: 5px;
        }

        .step-value {
            font-family: 'Consolas', 'Monaco', monospace;
            color: #333;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔢 16进制转换工具</h1>
        <p class="subtitle">大小端转换 + 十进制转换</p>

        <div class="error" id="errorBox"></div>

        <div class="input-group">
            <label for="hexInput">输入16进制数据（空格分隔）</label>
            <input type="text" id="hexInput" placeholder="例如: 54 7E F4 1E" autofocus>
        </div>

        <div class="button-group">
            <button class="btn-convert" onclick="convert()">🔄 转换</button>
            <button class="btn-clear" onclick="clearAll()">🗑️ 清空</button>
        </div>

        <div id="results" style="display: none;">
            <div class="result-box" style="display:none">
                <div class="result-label">大小端转换后</div>
                <div class="result-value" id="reversedHex"></div>
            </div>

            <div class="result-box">
                <div class="result-label">十进制结果</div>
                <div class="result-value highlight" id="decimalResult"></div>
            </div>
        </div>

        <div class="example">
            <div class="example-title">📝 使用示例</div>
            <div class="example-content">
                <strong>输入：</strong>54 7E F4 1E<br>
                <strong>大小端转换：</strong>1EF47E54<br>
                <strong>十进制：</strong>519339604
            </div>
        </div>
    </div>

    <script>
        // 监听回车键
        document.getElementById('hexInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                convert();
            }
        });

        function convert() {
            const input = document.getElementById('hexInput').value.trim();
            const errorBox = document.getElementById('errorBox');
            const results = document.getElementById('results');

            // 清除之前的错误
            errorBox.style.display = 'none';
            errorBox.textContent = '';

            if (!input) {
                showError('请输入16进制数据');
                results.style.display = 'none';
                return;
            }

            try {
                // 移除所有空格，然后按每2个字符分组
                const cleanInput = input.replace(/\s+/g, '').toUpperCase();
                
                // 验证是否为有效的16进制
                if (!/^[0-9A-F]+$/.test(cleanInput)) {
                    showError('输入包含无效字符，只能包含 0-9 和 A-F');
                    results.style.display = 'none';
                    return;
                }

                // 如果长度不是偶数，前面补0
                const paddedInput = cleanInput.length % 2 === 0 ? cleanInput : '0' + cleanInput;

                // 分割成字节数组
                const bytes = [];
                for (let i = 0; i < paddedInput.length; i += 2) {
                    bytes.push(paddedInput.substr(i, 2));
                }

                // 大小端转换（反转字节顺序）
                const reversedBytes = [...bytes].reverse();

                // 拼接为完整16进制字符串（不用空格或其他符号）
                const reversedHex = reversedBytes.join('');

                // 转换为10进制
                const decimal = parseInt(reversedHex, 16);

                // 显示结果（不用逗号分隔）
                document.getElementById('reversedHex').textContent = reversedHex;
                document.getElementById('decimalResult').textContent = decimal;

                results.style.display = 'block';

            } catch (error) {
                showError('转换失败: ' + error.message);
                results.style.display = 'none';
            }
        }

        function clearAll() {
            document.getElementById('hexInput').value = '';
            document.getElementById('results').style.display = 'none';
            document.getElementById('errorBox').style.display = 'none';
            document.getElementById('hexInput').focus();
        }

        function showError(message) {
            const errorBox = document.getElementById('errorBox');
            errorBox.textContent = '❌ ' + message;
            errorBox.style.display = 'block';
        }

        // 自动格式化输入（添加空格）
        document.getElementById('hexInput').addEventListener('input', function(e) {
            let value = e.target.value.toUpperCase().replace(/[^0-9A-F\s]/g, '');
            
            // 移除所有空格
            let cleaned = value.replace(/\s+/g, '');
            
            // 每2个字符添加一个空格
            let formatted = '';
            for (let i = 0; i < cleaned.length; i += 2) {
                if (i > 0) formatted += ' ';
                formatted += cleaned.substr(i, 2);
            }
            
            e.target.value = formatted;
        });
    </script>
</body>
</html>

